#
#  Effects.py
#  TurnShip
#
#  Created by Pete Lord on 22/12/2008.
#  Copyright (c) 2008 __MyCompanyName__. All rights reserved.
#
import Objects, Bounce, pygame, math, random, Projectiles, Options
pygame.font.init()

class Effect(Objects.Object):
    def __init__(self, parent, entities, r, life_time, surface):
        Objects.Object.__init__(self, entities, parent.x, parent.y, r)
        self.x = parent.x
        self.y = parent.y
        self.r = r 
        self.max_life_time = self.life_time = life_time
        self.surfaces = surface
        
    def draw(self):
        # the Ship class should _never_ be instantiated, this funcition will not work without self.textures
        # which is defined as part of the ship class itself.
        return self.surfaces[Options.ZOOM_LEVEL][0][self.r][self.current_frame], pygame.Rect(self.screen_location(), (Options.SPRITE_SIZE[Options.ZOOM_LEVEL], Options.SPRITE_SIZE[Options.ZOOM_LEVEL]))    
        
    def poll(self):
        self.life_time -= 1

        # removal of effects handled by entities
        for surface in self.surfaces[Options.ZOOM_LEVEL][0][self.r]:
            surface.set_alpha(255 * self.life_time / self.max_life_time)
            
class PawnCannonA(Effect):
    def __init__(self, parent, entities, surface):
        self.anim_frames = 7
        self.anim_frame_times = 5 
        Effect.__init__(self, parent, entities, parent.r, self.anim_frames * self.anim_frame_times, surface)
        
        # basic effect.
        self.anim_frames = 7
        self.anim_frame_times = 5 
        self.has_anim = True
        self.current_time = 0
        self.current_frame = 0 # init.
        
    def poll(self):
        self.life_time -= 1
        # removal of effects handled by entities
        for surface in self.surfaces[Options.ZOOM_LEVEL][0][self.r]:
            surface.set_alpha(255 * self.life_time / self.max_life_time)
            
        self.current_time += 1
        if self.current_time == self.anim_frame_times:
            self.current_time = 0
            self.current_frame += 1
            
class PawnCannonB(PawnCannonA):
    def __init__(self, parent, entities, surface):
        PawnCannonA.__init__(self, parent, entities, surface)
        
class ShipHit(Objects.Object):
    def __init__(self, parent, entities):
        Objects.Object.__init__(self, entities, parent.x, parent.y, 3)
        self.life_time = 10
        self.colour = (255, 0, 0)
        self.alpha = 150
        self.surface, self.position = parent.draw()
        self.poll()
        
    def poll(self):
        # Create a copy of it's sprite
        self.mask = self.surface.copy()
        # Create a red mask of the non-transparent areas
        self.mask.fill(self.colour, None, pygame.BLEND_ADD)
        # Lower the alpha levels
        self.mask.fill(self.colour + (self.alpha,), None, pygame.BLEND_RGBA_MULT)
        self.alpha -= 5
        self.life_time -= 1
        
    def draw(self):
        return (self.mask, self.position)    
        
class ShipHitCritical(Objects.Object):
    def __init__(self, parent, entities):
        Objects.Object.__init__(self, entities, parent.x, parent.y, 3)
        self.life_time = 10
        self.colour = (200, 150, 0)
        self.alpha = 255
        self.surface, self.position = parent.draw()
        self.poll()
        
    def poll(self):
        # Create a copy of it's sprite
        self.mask = self.surface.copy()
        # Create a red mask of the non-transparent areas
        self.mask.fill(self.colour, None, pygame.BLEND_ADD)
        # Lower the alpha levels
        self.mask.fill(self.colour + (self.alpha,), None, pygame.BLEND_RGBA_MULT)
        self.alpha -= 5
        self.life_time -= 1
        
    def draw(self):
        return (self.mask, self.position)  
        
class ShipDeath(Objects.Object):
    def __init__(self, parent, entities):
        Objects.Object.__init__(self, entities, parent.x, parent.y, 3)
        self.life_time = 120
        self.colour = (0, 0, 0)
        self.alpha = 240
        self.surface, self.position = parent.draw()
        self.poll()
        
    def poll(self):
        # Create a copy of it's sprite
        self.mask = self.surface.copy()
        # Create a red mask of the non-transparent areas
        self.mask.fill(self.colour, None, pygame.BLEND_ADD)
        # Lower the alpha levels
        self.mask.fill(self.colour + (self.alpha,), None, pygame.BLEND_RGBA_MULT)
        self.alpha -= 2
        self.life_time -= 1 
        
    def draw(self):
        return (self.mask, self.position)     
            
class FloatyText(Effect):
    font = pygame.font.Font(pygame.font.match_font('georgia'), 12)
    def __init__(self, entities, colour, text, ship):
        self.font.set_bold(True)
        self.surface = self.font.render(text, True, colour)
        Effect.__init__(self, ship, entities, 3, 20, self.surface)
        self.maxlife = self.life_time
        self.xy_mod.push(10, 3)
        
    def poll(self):
        self.life_time -= 1
        if self.life_time % 6 == 0:
            self.xy_mod.poll()
        #self.surface.set_alpha(255 * self.life_time / self.maxlife)
        #print self.surface.get_alpha()

    def draw(self):
        x, y = self.screen_location()
        x += (Options.TILE_WIDTH[Options.ZOOM_LEVEL] - self.surface.get_width()) / 2
        y += (Options.TILE_HEIGHT[Options.ZOOM_LEVEL] - self.surface.get_height()) / 2
        return self.surface, pygame.Rect((x, y - 10), self.surface.get_size())

